/**
 * @author phoenics@126com
 * @date 2017年11月8日 下午5:01:28
 * @version V1.0
 */

package com.jx.gocom.nlp.classify.webservice.restcontroller;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.jx.gocom.nlp.classify.webservice.DTO.KeywordSingleData;
import com.jx.gocom.nlp.classify.webservice.DTO.KeywordSingleResultData;
import com.jx.gocom.nlp.utils.JacksonUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 *
 */
@Api(value = "与中心词有关的API")
@RestController
@RequestMapping("/nlp/word")
public class KeyWord {
	private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(KeyWord.class);
	//private static String nlpservice="";
	@Autowired
    RestTemplate restTemplate;
	@Resource(name="deepService")
	String deepService;
	@ApiOperation(value = "获取单条中心词", notes = "")  
	@RequestMapping(value = "/keyword/single", method = RequestMethod.POST)
	@ResponseStatus(HttpStatus.OK)
	public KeywordSingleResultData keywordSingle(
			@RequestParam(value="token",  required=true) String wptoken,
			@RequestBody   KeywordSingleData keywordSingleData
			) {
		//logger.info("KeywordSingleData===keywordSingle==={}",keywordSingleData.getText());
		return takeKeyWords(keywordSingleData);
	}
	@ApiOperation(value = "获取多条中心词", notes = "")  
	@RequestMapping(value = "/keyword/multi", method = RequestMethod.POST)
	@ResponseStatus(HttpStatus.OK)
	public List<KeywordSingleResultData> keywordSingle(
			@RequestParam(value="token",  required=true) String wptoken,
			@RequestBody   List<KeywordSingleData> keywordSingleDatas
			) {
		return keywordSingleDatas.stream().map(this::takeKeyWords).collect(Collectors.toList());
	}
	private KeywordSingleResultData takeKeyWords(KeywordSingleData keywordSingleData) {
		String jsonStr=restTemplate.postForObject(deepService,keywordSingleData,String.class );
		return JacksonUtil.readValue(jsonStr, KeywordSingleResultData.class);
	}
}
